#!/usr/bin/env python3

import distutils.command.install
import distutils.core
import imp
import os
import Cython.Distutils

config = imp.load_source('config', os.path.join('kt', 'config.py'))

# You have to have the FontForge extension plugin installed already
# for this to work. FIX/TODO: Find a way to make that not so.
fontforge_so = os.path.join(config.kt_packages_dir, 'fontforge.so')

if os.getenv('CFLAGS') is None:
    os.environ['CFLAGS'] = config.python_package_CFLAGS

class my_install(distutils.command.install.install):
    def initialize_options(self):
        distutils.command.install.install.initialize_options(self)
        self.install_purelib = os.path.join(config.lib_dir, 'python' + config.pyver, 'site-packages')
        self.install_platlib = os.path.join(config.lib_dir, 'python' + config.pyver, 'site-packages')
        self.install_headers = os.path.join(config.include_dir, 'python' + config.pyver, 'kt')
        self.install_scripts = config.bin_dir
        self.install_data = config.home

distutils.core.setup(
    name = 'kt',
    version = config.kt_version,
    description = 'Kompostilo Typesetter',
    packages    = ['kt'],

    cmdclass = {
        'build_ext': Cython.Distutils.build_ext,
        'install'  : my_install,
        },

    ext_modules = [
        distutils.core.Extension(
            'kt.crypto', ['kt/crypto.pyx', 'kt/crypto.pxd'],
            libraries = ['gcrypt'],
            ),
        distutils.core.Extension(
            'kt.image', ['kt/image.pyx', 'kt/image.pxd'],
            libraries = ['MagickWand', 'MagickCore'],
            include_dirs = config.imagemagick_include_dirs,
            ),
        distutils.core.Extension(
            'kt.ot_features', ['kt/ot_features.pyx'],
            include_dirs = config.fontforge_include_dirs,
            extra_compile_args = ['-DHAVE_CONFIG_H'],
            extra_link_args = [fontforge_so],
            ),
        distutils.core.Extension(
            'kt.pdf_crypto', ['kt/pdf_crypto.pyx'],
            ),
        distutils.core.Extension(
            'kt.pdfobj', ['kt/pdfobj.pyx'],
            ),
        distutils.core.Extension(
            'kt.tfm', ['kt/tfm.pyx'],
            ),
        distutils.core.Extension(
            'kt.libff', ['kt/libff.pyx', 'kt/libff.pxd'],
            include_dirs = config.fontforge_include_dirs,
            extra_compile_args = ['-DHAVE_CONFIG_H'],
            extra_link_args = [fontforge_so],
            ),
        ],
    )
